#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>

#define DEV_NMAE "/dev/hello_led"

#define HELLO_DBG
#ifdef HELLO_DBG
#define hello_dbg(fmt, ...) printf("[%s][%d]" fmt "\r\n", __func__, __LINE__, ##__VA_ARGS__)
#else
#define hello_dbg(fmt, ...)
#endif

int main(int argc, char *argv[])
{
    int fd;
    int len;
    int value;

    hello_dbg("");

    if (argc != 2)
    {
        hello_dbg("./test_hello_nenopi_led.bin <on|off|read>");
        return -1;
    }

    if (strcmp(argv[1], "on") == 0)
    {
        value = 1;
    }
    else if (strcmp(argv[1], "off") == 0)
    {
        value = 0;
    }
    else if (strcmp(argv[1], "read") == 0)
    {
        value = 2;
    }
    else
    {
        hello_dbg("./test_hello_nenopi_led.bin <on|off|read>");
        return -1;
    }

    fd = open(DEV_NMAE, O_RDWR);
    if (fd < 0)
    {
        hello_dbg("open %s error, erron:%d:%s", DEV_NMAE, errno, strerror(errno));
        return -1;
    }

    if (value == 0 || value == 1)
    {
        len = write(fd, &value, sizeof(value));
        hello_dbg("write return len:%d", len);
    }
    else if (value == 2)
    {
        len = read(fd, &value, sizeof(value));
        hello_dbg("read %d return len:%d, %d", sizeof(value), len, value);
    }

    close(fd);

    return 0;
}
